package com.north.light.androidutils.canvas.examprogress.progress;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.north.light.androidutils.canvas.examprogress.ExamProgressBarInfo;

/**
 * FileName: ExamProgressBarView
 * Author: lzt
 * Date: 2022/7/7 9:06
 * 自定义进度条--文字在最右侧显示
 * 禁止了硬件加速
 */
public class ExamProgressBarView extends View implements ExamProgressBarViewApi {
    private boolean mInit = false;
    //绘制的信息
    private ExamProgressBarInfo mDrawInfo;

    private Paint mPaint;

    //控件绘制宽高--区别于外层控件高度
    private int mViewWidth, mViewHeight;

    //控件绘制高度
    private int mViewMeasureWidth, mViewMeasureHeight;

    public ExamProgressBarView(Context context) {
        super(context);
    }

    public ExamProgressBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initParams();
    }

    public ExamProgressBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParams();
    }

    public int getViewHeight() {
        return mViewHeight;
    }

    public int getViewWidth() {
        return mViewWidth;
    }

    public int getViewMeasureWidth() {
        return mViewMeasureWidth;
    }

    public int getViewMeasureHeight() {
        return mViewMeasureHeight;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        initParams();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawInfo(canvas);
    }

    /**
     * 初始化参数
     */
    private void initParams() {
        if (mPaint == null) {
            mPaint = new Paint();
        }
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(1);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setTextAlign(Paint.Align.LEFT);
        mPaint.setTextSize(dp2px(12));
    }


    /**
     * 绘制信息
     */
    private void drawInfo(Canvas canvas) {
        if (!mInit || mDrawInfo == null || mPaint == null) {
            return;
        }

        canvas.save();
        canvas.translate(0, (getViewMeasureHeight() / 2.0f) - (getViewHeight() / 2.0f));
        int bgRadius = dp2px(mDrawInfo.getBgRadius());
        int radius = dp2px(mDrawInfo.getRadius());
        mPaint.setShader(null);
        mPaint.setStyle(Paint.Style.FILL);
        //绘制背景--------------------
        mPaint.setColor(getResources().getColor(mDrawInfo.getBgColor()));
        canvas.drawRoundRect(0, 0, getViewWidth(), getViewHeight(), bgRadius, bgRadius, mPaint);
        //绘制进度--------------------
        //混合模式切除边角
        float mDrawProgress = (getViewWidth() * 1.0f / 100) * (Math.min(mDrawInfo.getProgress(), 100));
        mPaint.setShader(new LinearGradient(0, 0, mDrawProgress, getViewHeight(),
                getResources().getColor(mDrawInfo.getProgressStartColor()),
                getResources().getColor(mDrawInfo.getProgressEndColor()),
                Shader.TileMode.CLAMP
        ));
        RectF rectProgress = new RectF(0, 0, mDrawProgress, getViewHeight());
        canvas.drawRoundRect(rectProgress, radius, radius, mPaint);
        //绘制加粗的边框--------------------
        mPaint.setShader(null);
        mPaint.setStyle(Paint.Style.STROKE);
        int stokeSize = dp2px(mDrawInfo.getProgressStokeSize());
        mPaint.setColor(getResources().getColor(mDrawInfo.getProgressStokeColor()));
        mPaint.setStrokeWidth(stokeSize);
        canvas.drawRoundRect(0, 0, getViewWidth(), getViewHeight(), bgRadius, bgRadius, mPaint);
        //绘制一个阴影--------------------
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(mDrawInfo.getProgressShadowColor()));
        int shadowHeight = dp2px(mDrawInfo.getProgressShadowHeight());
        canvas.drawRoundRect(0, getViewHeight(), getViewWidth(),
                getViewHeight()+shadowHeight, bgRadius, bgRadius, mPaint);

        canvas.restore();
    }


    private int dp2px(int dpValue) {
        return (int) getContext().getResources().getDisplayMetrics().density * dpValue;
    }


    //外部调用方法---------------------------------------------------------------------------------

    @Override
    public void setProgress(ExamProgressBarInfo info) {
        //view初始化后才设置
        this.postDelayed(new Runnable() {
            @Override
            public void run() {
                mViewMeasureHeight = getMeasuredHeight();
                mViewMeasureWidth = getViewMeasureWidth();
                mDrawInfo = info;
                mInit = true;
                mViewWidth = getMeasuredWidth();
                mViewHeight = dp2px(mDrawInfo.getProgressHeight());
//                mViewHeight = getMeasuredHeight();
                postInvalidate();
            }
        }, 200);
    }


}
